const BaseDao = require('./BaseDao');
const BlockAudit = require('../model/BlockAudit');
const { BlockAuditStatus } = require('../constants/DbEnum');
const db = require('../config/DBconnector');
const { Op } = require('sequelize');

/**
 * BlockAudit DAO
 */
class BlockAuditDao extends BaseDao {
  /**
   * constructor
   */
  constructor() {
    super(BlockAudit);
  }

  /**
   * findBlockUsers
   * @param {String} username
   * @return {Array}
   */
  findBlockUsers() {
    const where = {
      status: BlockAuditStatus.INIT,
    };
    const group = 'username';
    const attributes = [
      'username',
      [db.fn('count', 1), 'count'],
      [db.fn('group_concat', db.col('id')), 'ids'],
    ];

    return this.model.findAll({ where, group, attributes });
  }

  /**
   * @param {String} username
   * @return {Boolean}
   */
  async isBlocking(username) {
    const result = await this.findAll({ username, status: BlockAuditStatus.BLOCKING });

    if (result.length > 0) {
      return true;
    }

    return false;
  }
}

module.exports = new BlockAuditDao();
